#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

}

MainWindow::~MainWindow()
{
    delete ui;

}

void MainWindow::on_pushButton_clicked()
{
    DELTA_MODE deltaMode;
    if(ui->radioButton_previous->isChecked())
        deltaMode = DELTA_MODE_PREVIOUS_FRAME;
    else if (ui->radioButton_average->isChecked())
        deltaMode = DELTA_MODE_AVERAGE;
    else if (ui->radioButton_learnedframe->isChecked())
        deltaMode = DELTA_MODE_LEARNED_FRAME;

    POST_TRAITEMENT postTraitement;
    if(ui->radioButton_Sobel->isChecked())
        postTraitement = SOBEL_POST_TRAITEMENT;
    else if (ui->radioButton_Laplace->isChecked())
        postTraitement = LAPLACE_POST_TRAITEMENT;
    else if (ui->radioButton_NormalMap->isChecked())
        postTraitement = NORMALMAP_POST_TRAITEMENT;
    else
        postTraitement = NONE_POST_TRAITEMENT;

    DISPLAY_TYPE displayType;
    if(ui->radioButton_Source->isChecked())
        displayType = SOURCE_DISPLAY_TYPE;
    else if(ui->radioButton_Learned->isChecked())
        displayType = LEARNED_IMAGE_DISPLAY_TYPE;
    else if(ui->radioButton_Average->isChecked())
        displayType = AVERAGE_IMAGE_DISPLAY_TYPE;
    else if(ui->radioButton_Delta->isChecked())
        displayType = DELTA_IMAGE_DISPLAY_TYPE;
    else if(ui->radioButton_MDI->isChecked())
        displayType = MDI_DISPLAY_TYPE;

    _foreground = new ForegroundExtractor(displayType,deltaMode,postTraitement,ui->horizontalSlider->value());
    _foreground->setBlur(ui->checkBox->isChecked());
    _foreground->setThreshold(ui->checkBox_2->isChecked());
    _foreground->setThresholdValue(ui->horizontalSlider_2->value());
    _foreground->learn();

    QTimer* timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),SLOT(on_timeExedeed()));
    timer->start(15);
}

void MainWindow::on_timeExedeed()
{
    _foreground->update();
    ui->label->setPixmap(QPixmap::fromImage(_foreground->getFrame()));
}

void MainWindow::closeEvent(QCloseEvent *event) {
    delete _foreground;
}

void MainWindow::on_checkBox_2_clicked()
{
    _foreground->setThreshold(ui->checkBox_2->isChecked());
}

void MainWindow::on_checkBox_clicked()
{
    _foreground->setBlur(ui->checkBox->isChecked());
}

void MainWindow::on_horizontalSlider_2_actionTriggered(int action)
{
    _foreground->setThresholdValue(ui->horizontalSlider_2->value());
}

void MainWindow::on_radioButton_Learned_clicked()
{
    _foreground->_displayType = LEARNED_IMAGE_DISPLAY_TYPE;
}

void MainWindow::on_radioButton_Source_clicked()
{
    _foreground->_displayType = SOURCE_DISPLAY_TYPE;
}

void MainWindow::on_radioButton_Average_clicked()
{
    _foreground->_displayType = AVERAGE_IMAGE_DISPLAY_TYPE;
}

void MainWindow::on_radioButton_Delta_clicked()
{
    _foreground->_displayType = DELTA_IMAGE_DISPLAY_TYPE;
}

void MainWindow::on_radioButton_MDI_clicked()
{
    _foreground->_displayType = MDI_DISPLAY_TYPE;
}

void MainWindow::on_radioButton_None_clicked()
{
    _foreground->_postTraitement = NONE_POST_TRAITEMENT;
}

void MainWindow::on_radioButton_Laplace_clicked()
{
    _foreground->_postTraitement = LAPLACE_POST_TRAITEMENT;
}

void MainWindow::on_radioButton_Sobel_clicked()
{
    _foreground->_postTraitement = SOBEL_POST_TRAITEMENT;
}

void MainWindow::on_radioButton_NormalMap_clicked()
{
    _foreground->_postTraitement = NORMALMAP_POST_TRAITEMENT;
}

void MainWindow::on_radioButton_previous_clicked()
{
    _foreground->_deltaMode = DELTA_MODE_PREVIOUS_FRAME;
}

void MainWindow::on_radioButton_average_clicked()
{
    _foreground->_deltaMode = DELTA_MODE_AVERAGE;
}

void MainWindow::on_radioButton_learnedframe_clicked()
{
    _foreground->_deltaMode = DELTA_MODE_LEARNED_FRAME;
}

void MainWindow::on_pushButton_2_clicked()
{
    _foreground->_referenceSize = ui->horizontalSlider->value();
    _foreground->learn();
}
